package com.example.controller;

import com.example.common.Result;
import com.example.entity.Article;
import com.example.mapper.ArticleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.util.StrUtil;

import javax.annotation.Resource;
import java.util.*;
import java.math.BigDecimal;
import java.util.stream.Collectors;

/**
 * 帖子接口
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleMapper articleMapper;

    @PostMapping
    public Result<?> save(@RequestBody Article article) {
        article.setTime(new Date());
        articleMapper.insert(article);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Article article) {
        article.setTime(new Date());
        articleMapper.updateById(article);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        articleMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> findById(@PathVariable Long id) {
        return Result.success(articleMapper.selectById(id));
    }

    @GetMapping
    public Result<?> findAll() {
        List<Article> list = articleMapper.selectList(null);
        return Result.success(list);
    }

    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
                              @RequestParam(required = false, defaultValue = "") String user,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Article> query = Wrappers.<Article>lambdaQuery().orderByDesc(Article::getId);
        if (StrUtil.isNotBlank(name)) {
            query.like(Article::getTitle, name);
        }
        if (StrUtil.isNotBlank(user)) {
            query.and(w -> w.eq(Article::getAuthor, user));
        }
        IPage<Article> page = articleMapper.selectPage(new Page<>(pageNum, pageSize), query);
        return Result.success(page);
    }

    @GetMapping("/count")
    public Result<?> count() {
        List<Article> list = articleMapper.selectList(null);
        Map<String, List<Article>> collect = list.stream().collect(Collectors.groupingBy(Article::getAuthor));
        Map<String, Integer> praiseCount = new HashMap<>();
        Map<String, Integer> readCount = new HashMap<>();
        for (Map.Entry<String, List<Article>> entry : collect.entrySet()) {
            int praiseSum = entry.getValue().stream().mapToInt(Article::getPraise).sum();
            int readSum = entry.getValue().stream().mapToInt(Article::getReadCount).sum();
            praiseCount.put(entry.getKey(), praiseSum);
            readCount.put(entry.getKey(), readSum);
        }
        Map<String, Object> res = new HashMap<>();
        res.put("praiseCount", praiseCount);
        res.put("readCount", readCount);
        return Result.success(res);
    }


}
